package com.smzd.ccms.business;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smzd.ccms.dao.ChannelConfigDao;
import com.smzd.ccms.dao.ChannelDao;
import com.smzd.ccms.domain.Channel;
import com.smzd.ccms.domain.Site;
import com.smzd.core.dao.IDao;
import com.smzd.framework.core.business.AbstractFGService;
import com.smzd.framework.system.domain.Meeting;
import com.smzd.framework.utils.db.FGRowConverter;
import com.smzd.framework.utils.db.ORPage;
import com.smzd.framework.utils.db.RPage;

@Service
public class ChannelService extends AbstractFGService<Channel> {
	private static ChannelService instance;

	public static ChannelService getInstance() {
		return instance;
	}

	@Autowired
	private ChannelDao dao;
	@Autowired
	private ChannelConfigDao chCfgDao;

	@Override
	protected IDao<Channel> getDao() {
		return dao;
	}

	@Override
    public FGRowConverter<Channel> getConverter() {
		return converter;
	}

    private static FGRowConverter<Channel> converter = new FGRowConverter<Channel>() {
        @Override
        public Object[] convert(Channel elem) {
            Channel parent = elem.getParent();
            Object[] obj_parent;
            if(parent==null){
                obj_parent = null;
            }else{
                Integer id_parent = parent.getId();
                String name_parent = parent.getName();
                obj_parent = new Object[]{id_parent, name_parent};
            }
            Object[] fs = new Object[]{
                    elem.getId(),
                    elem.getName(),
                    elem.getDescription(),
                    elem.getPath(),
                    elem.getType(),
                    obj_parent,
                    elem.getBackgroundURI(),
                    elem.isTemplateChannel()
                };
                return fs;
        }
    };
    
    public List<Channel> getAllChannelsByMeetingId(int meetingId){
    	return dao.getAllChannelsByOwnerId(meetingId);
    }

	public List<Channel> getSubChannelsByParentId(int chId) {
		return dao.getSubChannelsByParentId(chId);
	}

	@PostConstruct
	void init() {
		instance = this;
	}

	public List<Channel> getSublingChannelsByPath(String sPath, String cPath) {
		return dao.getSublingChannelsByPath(sPath, cPath);
	}

	public String getTemplateChannels(Meeting owner) {
		ORPage<Meeting, Channel> page = new ORPage<Meeting, Channel>();
		page.setOwner(owner);
		dao.getTemplateChannels(page);
		List<Channel> tchs = page.getElems();
		page.setPageSize(tchs.size());
		page.setTotalCount(tchs.size());
		return page.toFlexiGridJsonString(converter);
	}

	public void resetChannelPositionsForSite(Site site) {
		List<Channel> channels = dao.getAllChannelsByOwnerId(site.getOwner().getId());
		List<Channel> roots = new ArrayList<Channel>();
		for(Channel ch1 : channels){
			ch1.setSubChannels(new ArrayList<Channel>());
			if(ch1.getParent()==null){
				roots.add(ch1);
			}
			for(Channel ch2 : channels){
				if(ch2.getParent()!=null&&ch2.getParent().getId().intValue()==ch1.getId().intValue()){
					ch2.setParent(ch1);
					ch1.getSubChannels().add(ch2);
				}
			}
		}
		resetChannelPositions(roots);
		for(Channel ch : channels){
			dao.updateSequence(ch);
		}
	}

	private void resetChannelPositions(List<Channel> roots) {
		for(int i=0; i<roots.size(); i++){
			Channel ch = roots.get(i);
			ch.setSequence(i);
			resetChannelPositions(ch.getSubChannels());
		}
	}

	public void updateChannelPosition(Channel ch) {
		dao.updateSequence(ch);
	}

	public Channel getChannelByPath(String siteStr, String channelStr) {
		return dao.getChannelByPath(siteStr, channelStr);
	}

	public Channel getChannelByPathId(String siteStr, int chId) {
		return dao.getChannelByPathId(siteStr, chId);
    }
    
	/**
	 * 只更新栏目的类型
	 * @param origin
	 */
	public void updateChannelType(Channel channel) {
	    dao.updateType(channel);
    }

	/**
	 * 禁用栏目所有的视角配置，为了按照模板复制新的栏目配置过来。
	 * @param channel
	 */
	public void removeChannelConfigs(Channel channel) {
		chCfgDao.removeChannelConfigsForChannel(channel.getId()); 
    }
 
 	/**
	 *  获取面包屑
	 * @param chId
	 * @return
	 */
	public Channel getBreadcrumbChannels(int chId) {
	    return dao.getBreadcrumbChannelsByChId(chId);
    }

	/**
	 * 获取面包屑
	 * @param sPath
	 * @param cPath
	 * @return
	 */
	public Channel getBreadcrumbChannelsByPath(String siteStr, String channelStr) {
	    return dao.getBreadcrumbChannelsByPath(siteStr, channelStr);
    }

	/**
	 * 获取所有的频道，并频道中最新一条的文章。 <br>
	 * 所有的频道必须来自同一个父级目录，这是按照策划规定的。<br>
	 * 父级目录放在page.id当中。如果没有给值，则不用遵守这一规定了。
	 * 
	 * @param page
	 */
	public RPage<Channel> getChannelsWithLastArticleByPage(RPage<Channel> page) {
		return dao.getChannelsWithLastArticleByPage(page);
	}
}